package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivity;
import com.ecoolex.ying.space.entity.EstateProjectActivity.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityExample.Criteria;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityManager {
	
    @Autowired
    private EstateProjectActivityDAO dao;
    
    public EstateProjectActivity get(Integer projectId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(projectId, EstateProjectActivity.NOT_DELETED);
	}

	public Pagination<EstateProjectActivity> query(Boolean online,Sorting[] sorts, Page page) {
		EstateProjectActivityExample example = new EstateProjectActivityExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNull(online)) {
			criteria.andOnlineEqualTo(online);
		}
		criteria.andDeletedEqualTo(EstateProjectActivity.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public EstateProjectActivity getOnline(Integer projectId) {
		EstateProjectActivityExample example = new EstateProjectActivityExample();
		Criteria criteria = example.createCriteria();
		criteria.andProjectIdEqualTo(projectId).andOnlineEqualTo(true).andDeletedEqualTo(EstateProjectActivity.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Pagination<EstateProjectActivity> queryOnline(Page page) {
		EstateProjectActivityExample example = new EstateProjectActivityExample();
		Criteria criteria = example.createCriteria();
		criteria.andOnlineEqualTo(true).andDeletedEqualTo(EstateProjectActivity.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	private void verify(EstateProjectActivity record) {
		BizAssert.isTrue(Check.notNullOrEmpty(record.getName()),ResultCode.VERIFY.build(10), "项目名称为空");
		BizAssert.isTrue(Check.notNullOrEmpty(record.getIntroduction()),ResultCode.VERIFY.build(11), "项目介绍为空");
	}
	
	public EstateProjectActivity add(EstateProjectActivity record, String createUser) throws BizException {
		verify(record);
		record.setOnline(false);
		record.setDeleted(EstateProjectActivity.NOT_DELETED);
		record.setCreateTime(Date.from(Instant.now()));
		record.setCreateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		dao.insert(record);
		return record;
	}

	public EstateProjectActivity edit(EstateProjectActivity record, String updateUser) throws BizException {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.online,Column.createTime,Column.createUser));
		return record;
	}

	public void delete(EstateProjectActivity record, String updateUser){
		record.setDeleted(EstateProjectActivity.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.deleted,Column.updateUser,Column.updateTime);
	}

	public void online(EstateProjectActivity record, String updateUser){
		record.setOnline(true);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.online,Column.updateUser,Column.updateTime);
	}

	public void offline(EstateProjectActivity record, String updateUser){
		record.setOnline(false);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.online,Column.updateUser,Column.updateTime);
	}
}